package com.ulinkle.ulinkle_muhf.uhf;

import android.content.Context;
import android.os.SystemClock;

import com.android.usbserial.util.LogUtils;
import com.speedata.libuhf.IUHFService;
import com.speedata.libuhf.UHFManager;
import com.ulinkle.ulinkle_muhf.R;
import com.ulinkle.ulinkle_muhf.libutils.PlaySoundPool;
import com.ulinkle.ulinkle_muhf.libutils.SharedXmlUtil;
import com.ulinkle.ulinkle_muhf.libutils.ToastUtil;
import com.ulinkle.ulinkle_muhf.model.UhfInfoBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: java类作用描述
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class UHFUtil {

    public final static String SERVER_PREFIX = "server_prefix";
    public final static String SERVER_SUFFIX = "server_suffix";
    public final static String SERVER_STOP_FLAG = "server_stop_flag";
    public final static String SERVER_PREFIX_CUSTOM = "server_prefix_custom";
    public final static String SERVER_SUFFIX_CUSTOM = "server_suffix_custom";
    public final static String SERVER_STOP_FLAG_CUSTOM = "server_stop_flag_custom";
    public final static String SERVER_IS_LONG_PRESS = "server_is_long_press";
    public final static String SERVER_IS_FILTER = "server_is_filter";
    public final static String SERVER_IS_ASCII = "server_is_ascii";
    public final static String UHF_FREQ = "uhf_freq";
    public final static String UHF_SESSION = "uhf_session";
    public final static String UHF_POWER = "uhf_power";
    public final static String UHF_INV_CON = "uhf_inv_con";
    public final static String UHF_INV_CON_START = "uhf_inv_con_start";
    public final static String UHF_INV_CON_SIZE = "uhf_inv_con_size";
    public final static String UHF_INV_TIME = "uhf_inv_time";
    public final static String UHF_INV_SLEEP = "uhf_inv_sleep";
    public final static String UHF_IS_STOP_TIME = "uhf_is_stop_time";
    public final static String UHF_M_STOP_TIME = "uhf_m_stop_time";
    public final static String ACTION_SEND_CUSTOM = "action_send_custom";
    public final static String ACTION_KEY_EPC = "action_key_epc";
    public final static String ACTION_KEY_TID = "action_key_tid";
    public final static String ACTION_KEY_RSSI = "action_key_rssi";
    public final static String KEY_TAG_FOUCS = "uhf_tag_foucs";
    /**
     * 快速模式是否支持附加数据
     */
    public final static String UHF_FASTMODE_EXTRA = "uhf_fastmode_enable_extradata";
    /**
     *  智能模式
     */
    public final static String UHF_SMART_MODE = "uhf_smart_mode";


    /**
     * 是否焦点显示epc
     */
    public final static String IS_FOCUS_SHOW = "isFocusShow";
    public final static String EPC_OR_TID = "focus_switch_tid";
    public static boolean isStart = false;
    public static boolean isOpenDev = false;
    public static  boolean isOpenServer = true;
    public static  int mPrefix = 3;
    public  static int mSuffix = 3;
    public static   int mStopFlag = 3;
    public static  boolean isLongDown = false;
    public static  String mStopTime = "10";
    public static  boolean isStopTime = false;

      /**
     * 是否过滤
     */
    public static boolean isFilter = false;

       /**
     * 是否启动快速模式
     * Whether to start fast mode
     */
    public static boolean isFastMode = false;
    /**
     * 缓存列表
     */
    public static List<UhfInfoBean> list = new ArrayList<>();

    PlaySoundPool playSoundPool;
    private static UHFUtil instance;
    private UHFUtil(){}
    /**
     * 对外为唯一实例
     * @return uhfManager
     */
    public static UHFUtil getInstance(){
        if (instance==null){
            instance=new UHFUtil();
        }
        return instance;
    }


    private IUHFService iuhfService;
    public IUHFService getIuhfService() {
        return iuhfService;
    }

    public void setIuhfService(Context applicationContext) {
        try {
            iuhfService = UHFManager.getUHFService(applicationContext);
            LogUtils.d("iuhfService初始化: " + iuhfService);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.showLong(applicationContext, applicationContext.getResources().getString(R.string.dialog_module_none));
            LogUtils.d("iuhfService初始化: 失败" + e.getMessage());
        }

    }


    public void initParam(Context applicationContext) {
        int i;
        i = iuhfService.setFreqRegion(SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.UHF_FREQ, 0));
        LogUtils.d( "===isFirstInit===setFreqRegionResult:" + i);
        SystemClock.sleep(600);
        LogUtils.d( "===isFirstInit===FreqRegion:" + iuhfService.getFreqRegion());
        i = iuhfService.setAntennaPower(SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.UHF_POWER, 30));
        LogUtils.d(  "===isFirstInit===setAntennaPowerResult:" + i);
        SystemClock.sleep(100);
        if (!UHFManager.getUHFModel().equals(UHFManager.FACTORY_YIXIN)) {
            i = iuhfService.setQueryTagGroup(0, SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.UHF_SESSION, 0), 0);
            LogUtils.d(  "===isFirstInit===setQueryTagGroupResult:" + i);
        }
        SystemClock.sleep(100);
        i = iuhfService.setInvMode(SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.UHF_INV_CON, 0),
                SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.UHF_INV_CON_START, 0),
                SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.UHF_INV_CON_SIZE, 0));
        LogUtils.d( "===isFirstInit===setInvModeResult:" + i);
        if (UHFManager.getUHFModel().contains(UHFManager.FACTORY_XINLIAN)) {
            iuhfService.setLowpowerScheduler(SharedXmlUtil.getInstance(applicationContext)
                    .read(UHFUtil.UHF_INV_TIME, 50), SharedXmlUtil.getInstance(applicationContext)
                    .read(UHFUtil.UHF_INV_SLEEP, 0));
            iuhfService.setTagfoucs(SharedXmlUtil
                    .getInstance(applicationContext).read(UHFUtil.KEY_TAG_FOUCS, true));
        }
        SystemClock.sleep(100);
        UHFUtil.mPrefix = SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.SERVER_PREFIX, 3);
        UHFUtil.mSuffix = SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.SERVER_SUFFIX, 3);
        UHFUtil.mStopFlag = SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.SERVER_STOP_FLAG, 4);
        UHFUtil.isLongDown = SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.SERVER_IS_LONG_PRESS, false);
        UHFUtil.isFilter = SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.SERVER_IS_FILTER, false);
        isStopTime = SharedXmlUtil.getInstance(applicationContext).read(UHF_IS_STOP_TIME, false);
        mStopTime = SharedXmlUtil.getInstance(applicationContext).read(UHF_M_STOP_TIME, "10");
        LogUtils.d("cache UHFUtil.SERVER_STOP_FLAG:"+SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.SERVER_STOP_FLAG, 4));
        LogUtils.d("cache UHFUtil.UHF_POWER:"+ SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.UHF_POWER, 30));
        LogUtils.d("cache UHFUtil.UHF_FREQ:"+SharedXmlUtil.getInstance(applicationContext).read(UHFUtil.UHF_FREQ, 0));
    }

}
